/*
* # ManifestContentDownloader.js
* # NGJurassic
* #
* # Created by Washaq.Majeed on 5/30/11.
* # Copyright 2011 __MyCompanyName__. All rights reserved.
*/
exports.ManifestContentDownloader = Core.Class.subclass({
														
	classname: 'ManifestContentDownloader',
	
	initialize: function(url, localPath, filename, ref) {
		this._url = url;
		this._localPath = localPath;
		this._filename = filename;
		this._reference = ref;
		
		this.doDownload();
	},
	doDownload: function() {
		//this._reference.progressScreen._prevProgressValue = 0;
		this.dl = new Network.DownloadManifest(); 
		this.dl.start(this._url, this._localPath, this._filename, this._progressCb.bind(this), this._doneCb.bind(this));
	},
	retry: function() {
		this.dl.start(this._url, this._localPath, this._filename, this._progressCb.bind(this), this._doneCb.bind(this));
	},
	_progressCb: function ( completedRequests, totalRequests ) {
		if(completedRequests <= totalRequests){
			var percentage = 0;
			if(totalRequests > 0) {
				percentage = (parseInt((completedRequests/totalRequests)*100));
			}
			else {
				percentage = 100;
			}
			this._reference.updateMenifestProgress(percentage);
		}
		else {
			
		}
		//this._reference.updateMenifestProgress(completedRequests, totalRequests);
	},
	_doneCb: function ( err, manifest ) {
		
		if(err) {
			this._failureUIAlert = new GAUIAlertView(Core.Localization.getString("warning"),Core.Localization.getString("MM_REQUIRE_INTERNET_CONNECTION"),this,null,Core.Localization.getString("retry"));
			console.log("zaki::::: MM_REQUIRE_INTERNET_CONNECTION _doneCb");
			this._failureUIAlert.setTagValue(1111);
			this._failureUIAlert.show();
			
			//var errorAlert = new UI.AlertDialog();
//			errorAlert.setTitle(ALERT_MSG_FAILURE_TITLE);
//			errorAlert.setText(ALERT_MSG_MANIFEST_DOWNLOAD_FAILURE);
//			errorAlert.setChoices(["Retry"]);
//			var self = this;
//			errorAlert.onchoice = function(ret) {
//				errorAlert.hide();
//				self.retry();
//				errorAlert.destroy();
//			};
//			errorAlert.show();
		}	
		else {
			this._reference.doneDownloading();
		}
	},
	clickedButtonAtIndex: function(buttonIndex,alerWindow)
	{
		
		if (buttonIndex == 0) 
		{
			if(alerWindow.getTagValue() == 1111)
			{
				this.retry();
				
			}
			
			
		} 
		else if (buttonIndex == 1)
		{
			//NgLogE("Cancel button clicked");
		}
		
	},
	destroy: function() {
		if(this.dl)
		{
			this.dl.destroy();
			this.dl = null;
		}
	}
});

	